package com.lxm.controller;


import com.lxm.entity.Category;
import com.lxm.entity.ResultEntity;
import com.lxm.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author lxm
 * @since 2021-11-23
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService cs;

    //1.根据父类别的编号查询该类别下面所有的子类别
    @GetMapping("/selectChildrenCategory/{categoryId}")
    public ResultEntity<List<Category>> selectChildren(@PathVariable int categoryId){
        List<Category> categories = cs.selectChildren(categoryId);
        if (categories==null){
            return ResultEntity.error();
        }
        return ResultEntity.ok(categories);
    }
    //2.添加类别
    @PostMapping("/add")
    public ResultEntity addCate(@RequestBody Category category){
        boolean sign = cs.save(category);
        if(sign){
            return ResultEntity.ok();
        }
        return ResultEntity.error();
    }
    //3.删除类别
    @DeleteMapping("/delete/{categoryId}")
    public ResultEntity deleteCate(@PathVariable int categoryId){
        Boolean sign = cs.deleteCate(categoryId);
        if(sign){
            return ResultEntity.ok();
        }
        return ResultEntity.error("有子目录不能删");
    }
    //4.修改类别
    @PutMapping("/update")
    public ResultEntity update(@RequestBody Category category){
        boolean sign = cs.updateById(category);
        if(sign){
            return ResultEntity.ok();
        }
        return ResultEntity.error("修改类别失败");
    }
    //5.查询整个类别树
    @GetMapping("/selectTree")
    public ResultEntity<List<Category>> selectTree(){
        List<Category> categories = cs.selectTree();
        if (categories.size()==0){
            return ResultEntity.error();
        }
        return ResultEntity.ok(categories);
    }
}

